package ClimateControlProcessor;

import android.accounts.AccountManager;
import android.view.View;
import ClimateControlProcessor.ClimateObjects.Constant;
import ClimateControlProcessor.ClimateObjects.Constant.FANDIRECTION;
import ClimateControlProcessor.ClimateObjects.Constant.MODE;
import ClimateControlProcessor.ClimateObjects.CurrentVehicleStatus;
import ProfileParser.ProfileParser;
import AC.Controller.*; 

public class Process {
	
	/// Change Mode of the AC system
	public void processMode(){
		
	}
	public static void onClickAuto(View v ){	
		if(Constant.currentACConfig.Mode == MODE.Debug)
			Constant.currentACConfig = Constant.currentDebugACConfig;
		Constant.currentACConfig.Mode = MODE.Auto;		
		Constant.autoFlag = true;
	}
	public static void onClickManual(View v ){
		if(Constant.currentACConfig.Mode == MODE.Debug)
			Constant.currentACConfig = Constant.currentDebugACConfig;
		Constant.currentACConfig.Mode = MODE.Manual;	
		Constant.autoFlag = false;
	}
	public static void onClickDebug(View v ){
		Constant.currentACConfig.Mode = MODE.Debug;
		Constant.autoFlag = false;
		CurrentVehicleStatus.copyObject(Constant.currentACConfig);
		setAllOn();
		processAirFlowDirection(FANDIRECTION.All);
	}
	
	public static void processAirFlowDirection(FANDIRECTION i){
		switch(i){
		case HeadOnly:
			Constant.head =true;
			Constant.foot =false;
			Constant.defrost =false;
			break;
		case HeadNFoot:
			Constant.head =true;
			Constant.foot =true;
			Constant.defrost =false;
			break;
		case FootOnly:
			Constant.head =false;
			Constant.foot =true;
			Constant.defrost =false;
			break;
		case DefrostNFoot:
			Constant.head =false;
			Constant.foot =true;
			Constant.defrost =true;
			break;
		case Defrostonly:
			Constant.head =false;
			Constant.foot =false;
			Constant.defrost =true;
			break;
		case Recirculation:
			if(Constant.reciculation)
				Constant.reciculation =false;
			else
				Constant.reciculation =true;
			break;
		default:
			Constant.head =true;
			Constant.foot =true;
			Constant.defrost =true;
			Constant.reciculation =true;
		}
	}	
	
	public static void processMonovalve(int vehicleTemp){
		int userDesiredTemp = Constant.desiredConfig.getDesiredTemp();
		int tempDiff= (vehicleTemp - userDesiredTemp);
		// control monovalve 
		if(tempDiff<-10)
			Constant.currentACConfig.currMonoValve.setExpandedValue(Constant.VALUE100); //set the mono valve to 100 percent
		else if(tempDiff<-8)
			Constant.currentACConfig.currMonoValve.setExpandedValue(Constant.VALUE80); //set the mono valve to 80 percent
		else if(tempDiff<-6)
			Constant.currentACConfig.currMonoValve.setExpandedValue(Constant.VALUE60);//set the mono valve to 60 percent
		else if(tempDiff<-4)
			Constant.currentACConfig.currMonoValve.setExpandedValue(Constant.VALUE40);//set the mono valve to 40 percent
		else if(tempDiff<-2)
			Constant.currentACConfig.currMonoValve.setExpandedValue(Constant.VALUE20);//set the mono valve to 20 percent
		else
			Constant.currentACConfig.currMonoValve.setExpandedValue(Constant.VALUE0);//turn off the mono valve
					
	}
	
	public static void processBlowerMotor(int vehicleTemp){
		int userDesiredTemp = Constant.desiredConfig.getDesiredTemp();
		int tempDiff= (userDesiredTemp - vehicleTemp);
		if (Math.abs(tempDiff)>=10){
			Constant.currentACConfig.currBlowMotor.setCurrentSpeed(Constant.VALUE100);//it is the 100% speed
		}
		else if(Math.abs(tempDiff)>=8)
			Constant.currentACConfig.currBlowMotor.setCurrentSpeed(Constant.VALUE80);//it is the 80% speed
		else if(Math.abs(tempDiff)>=6)
			Constant.currentACConfig.currBlowMotor.setCurrentSpeed(Constant.VALUE60);//it is the 60% speed
		else if(Math.abs(tempDiff)>=4)
			Constant.currentACConfig.currBlowMotor.setCurrentSpeed(Constant.VALUE40);//it is the 40% speed
		else if(Math.abs(tempDiff)>0)
			Constant.currentACConfig.currBlowMotor.setCurrentSpeed(Constant.VALUE20);//it is the 20% speed
		else
			Constant.currentACConfig.currBlowMotor.setCurrentSpeed(Constant.VALUE0);
	}
	
	public static void processCompressorClutch(int vehicleTemp){
		int userDesiredTemp = Constant.desiredConfig.getDesiredTemp();
		if(userDesiredTemp<vehicleTemp){
			Constant.currentACConfig.currComClutch.setStatus(true); //turn on the compressor clutch
		}
		else{
			Constant.currentACConfig.currComClutch.setStatus(false); //turn off the compressor clutch
		}
	}
	
	public void processTurnOnOffAC(){
		
	}
	
	/*
	 * Called when 
	 * the connection established between IOIO and Android
	 */
	public static void initialMode(String username){
		ProfileParser profileParser = new ProfileParser();
		profileParser.loadProfile(username);
	}
	
	public static void autoCheckConfig(int vehicleTemp){
		switch(Constant.currentACConfig.Mode){
		case Auto:
			// Control the fan's speed
			processBlowerMotor(vehicleTemp);
			
			// control compressor clutch(
			processCompressorClutch(vehicleTemp);
			
			// control monovalve 
			processMonovalve(vehicleTemp);
			
			break;
		case Manual:
			// control compressor clutch(
			processCompressorClutch(vehicleTemp);
			
			// control monovalve 
			processMonovalve(vehicleTemp);
			
			break;
		case Debug:
			break;
		
		default:
		}
	}
	
	/*
	 * Set all ports on
	 */
	public static void setAllOn(){
		Constant.currentACConfig.airDirection= FANDIRECTION.All; 
		Constant.currentACConfig.currBlowMotor.setCurrentSpeed(100); 
		Constant.currentACConfig.currComClutch.setStatus(true); 
		Constant.currentACConfig.currMonoValve.setExpandedValue(100); 
		Constant.currentACConfig.Mode= MODE.Debug; 
	}
}
